package com.example;

import org.junit.Test;

import java.util.Optional;

public class OptionalDemos {

    public static void main(String[] args) {
        Optional<String> optional1 = Optional.of("Hello");
        Optional<String> optional2 = Optional.empty();

        System.out.println(optional1.get());
        System.out.println(optional2.orElse("OtherString"));
        System.out.println(optional2.orElseGet(() -> "This is a simple string"));
        optional2.orElseThrow(() -> new RuntimeException("This is Exception"));

    }
    @Test
    public void t1(){
        Optional<String> optional = Optional.empty();
        Optional<String> opt2 = Optional.of("This is a test String");
        optional.ifPresent(System.out::println);
        opt2.ifPresent(System.out::println);

        opt2.map(e -> e + "\nHelloWorld").ifPresent(System.out::println);
    }
}
